//
//  PEMChallengeTabCell.swift
//  RelationshipIntegration
//
//  Created by iOS-Czz on 2023/11/16.
//

import UIKit
import KFGradientProgressView
import SwiftAlertView

class PEMChallengeTabCell: UITableViewCell {
    @IBOutlet var progressView: GradientProgressView!
    @IBOutlet var completedIcon: UIImageView!
    @IBOutlet var infoLabel: UILabel!
    @IBOutlet var dateLabel: UILabel!
    @IBOutlet var nameLabel: UILabel!
    @IBOutlet var rewardsImage: UIImageView!
    var refreshBlock: (() -> Void)?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

   
    @IBAction func clickDeleteAction(_ sender: Any) {
        SwiftAlertView.show(title: "Title",
                            message: "Are you sure you want to delete this challenge？",
                            buttonTitles: "Cancel", "OK") {
            $0.style = .auto
        }
        .onButtonClicked { _, buttonIndex in
            if buttonIndex == 1 {
                self.model?.deleteToDB()
                self.refreshBlock?()
            }
        }
    }
    var model: PEMChallengeModel? {
        didSet {
            self.nameLabel.text = model?.name
            self.dateLabel.text = model?.date
            progressView.layer.cornerRadius = 3
            progressView.progressCornerRadius = 3
            progressView.progressColors = [.green]
            progressView.animationDuration = 1
            progressView.timingFunction = CAMediaTimingFunction(name: .default)
            /// 0 duration 1 distance 2 step 3 energy
            var str = ""
            switch model?.type {
            case 0:
                str = "h"
            case 1:
                str = "m"
            case 2:
                str = ""
            case 3:
                str = "kcal"
            default:
                break
            }
            self.infoLabel.text = String(format: "%.0f%@/%.0f%@", model!.progress,str,model!.target,str)
            progressView.setProgress(Float(model!.progress / model!.target), animated: true)
            self.rewardsImage.image = UIImage.loadImage(model!.rewards)
            self.completedIcon.image = UIImage(named: model!.completed ? "bg_challenge_completed":"bg_challenge_uncompleted")
        }
    }
}
